package cn.edu.cug.cs.gtl.plugins.device.common;

import io.minio.MinioClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtils {
    public boolean connectionForHDFS(String url, String user,String password){
        //连接HDFS
        try{
            Configuration conf = new Configuration();
            conf.set("dfs.client.use.datanode.hostname", "true");
            //conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");

            String s = "hdfs://" + url ;

            FileSystem fs;
            if (user != null && !user.isEmpty()) {
                fs = FileSystem.get(new URI(s), conf, user);
            } else {
                fs = FileSystem.get(conf);
            }

            // Perform a simple operation to test the connection
            boolean isConnected = fs.exists(fs.getHomeDirectory());

            // Close the FileSystem
            fs.close();

            return isConnected;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean connectionForMinIo(String url, String user, String password) {
        //连接MinIO
        try {
            // 创建MinioClient对象
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(url)
                    .credentials(user, password)
                    .build();

            // 检查是否能够成功连接到MinIO服务器
            boolean isConnected = minioClient.listBuckets().iterator().hasNext();

            return isConnected;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean connectionForPostgreSQL(String url, String user, String password) {
        //连接PostgreSQL
        try {
            Class.forName("org.postgresql.Driver");
            Connection connection = DriverManager.getConnection(url, user, password);
            boolean isConnected = connection.isValid(1000);

            connection.close();

            return isConnected;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean connectionForMySQL(String url, String user, String password) {
        //连接MySQL
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, user, password);
            boolean isConnected = connection.isValid(1000);

            connection.close();

            return isConnected;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean connectionForOracle(String url, String user, String password) {
        //连接Oracle
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection connection = DriverManager.getConnection(url, user, password);
            boolean isConnected = connection.isValid(1000);

            connection.close();

            return isConnected;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean connectionForMongoDB(String url, String user, String password) {
        //连接MongoDB
        try {
            Class.forName("com.mongodb.MongoClient");
            Connection connection = DriverManager.getConnection(url, user, password);
            boolean isConnected = connection.isValid(1000);

            connection.close();

            return isConnected;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
